package com.lego.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lego.bean.Goods;
import com.lego.bean.Seller;
import com.lego.bean.User;
import com.lego.dao.BaseDao;
import com.lego.dao.SellerDao;

public class SellerDaoImpl implements SellerDao {

	@Override
	public Seller selcetSeller(int sellerId) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		Seller seller = null;
		String sql = "SELECT * FROM seller WHERE sellerId = ? ";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setObject(1, sellerId);
			System.out.println(ps);
			res = ps.executeQuery();
			if(res.next()) {
				seller = new Seller();
				seller.setSellerId(res.getInt("sellerId"));
				seller.setSellerImg(res.getString("sellerImg"));
				seller.setSellerName("sellerName");
				seller.setBrandName(res.getString("brandName"));
				seller.setSellerPwd("sellerPwd");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return seller;
	}

	@Override
	public Seller selectBySellerName(String brandName) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		Seller seller = null;
		String sql = "SELECT * FROM seller WHERE brandName = ? ";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setObject(1, brandName);
			System.out.println(ps);
			res = ps.executeQuery();
			if(res.next()) {
				seller = new Seller();
				seller.setSellerId(res.getInt("sellerId"));
				seller.setSellerImg(res.getString("sellerImg"));
				seller.setSellerName("sellerName");
				seller.setBrandName(res.getString("brandName"));
				seller.setSellerPwd("sellerPwd");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return seller;
	}

	@Override
	public Seller selectSeller(String brandName, String sellerName) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		Seller seller = null;
		String sql = "SELECT * FROM seller WHERE brandName = ? ||  sellerName = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setObject(1, brandName);
			ps.setObject(2, sellerName);
			System.out.println(ps);
			res = ps.executeQuery();
			if(res.next()) {
				seller = new Seller();
				seller.setSellerId(res.getInt("sellerId"));
				seller.setSellerImg(res.getString("sellerImg"));
				seller.setSellerName("sellerName");
				seller.setBrandName(res.getString("brandName"));
				seller.setSellerPwd("sellerPwd");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return seller;
	}

	@Override
	public int addSeller(String sellerName, String sellerPwd, String brandName, String sellerImg) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "INSERT INTO seller(sellerName,sellerPwd,brandName,sellerImg) VALUES(?,?,?,?)";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, sellerName);
			ps.setString(2, sellerPwd);
			ps.setString(3, brandName);
			ps.setString(4, sellerImg);
			result = ps.executeUpdate();
		} catch (SQLException e) {			
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}

	@Override
	public int querySellerRow(String brandName) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		int totalRows = 0;
		String sql = "SELECT COUNT(*) AS totalRows FROM seller";
		if(brandName!=null&&!brandName.equals("")) {
			sql+=" WHERE brandName = ?";
		}
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			if(brandName!=null&&!brandName.equals("")) {
				ps.setString(1, brandName);
			}
			System.out.println(ps);
			res = ps.executeQuery();
			if(res.next()) {
				totalRows = res.getInt("totalRows");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return totalRows;
	}

	@Override
	public List<Seller> queryAllSeller(String brandName, int pageNum, int pageSize) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		List<Seller> list = new ArrayList<Seller>();
		List<Object> params = new ArrayList<Object>();
		String sql = "SELECT * FROM seller ";
		System.out.println(brandName);
		if(brandName!=null&&!brandName.equals("")) {
			sql+=" WHERE brandName = ? ";
			params.add(brandName);
		}
		sql+=" LIMIT ?,?";
		int newPageNum = (pageNum-1)*pageSize;
		params.add(newPageNum);
		params.add(pageSize);
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			for (int i = 0; i < params.size(); i++) {
				ps.setObject(i+1, params.get(i));
			}
			System.out.println(ps);
			res = ps.executeQuery();
			while(res.next()) {
				Seller seller = new Seller();
				seller.setBrandName(res.getString("brandName"));
				seller.setSellerId(res.getInt("sellerId"));
				seller.setSellerImg(res.getString("sellerImg"));
				seller.setSellerName(res.getString("sellerName"));
				seller.setSellerPwd(res.getString("sellerPwd"));
				list.add(seller);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return list;
	}

	@Override
	public int updateSellerPwd(int id, String nPwd) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		int rows = 0;
		String sql = "UPDATE seller SET sellerPwd = ? WHERE sellerId = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, nPwd);
			ps.setInt(2, id);
			System.out.println(ps);
			rows = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return rows;
	}

}
